package com.example.workflow.controller;


import com.example.workflow.entity.PdfFile;
import com.example.workflow.repository.PdfFileRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 黄历
 * @since 2024-03-14
 */
@RestController
//@RequestMapping("/pdf-files")
@RequiredArgsConstructor
public class PdfFilesController {
    private final PdfFileRepository pdfFileRepository;
    @PostMapping("/uploadPdf")
    public ResponseEntity<String> uploadFile(MultipartFile file) {
        if (file.isEmpty()) {
            return new ResponseEntity<>("请上传文件", HttpStatus.BAD_REQUEST);
        }

        try {
            PdfFile pdfFile = new PdfFile();
            pdfFile.setFileName(file.getOriginalFilename());
            pdfFile.setFileContent(file.getBytes());
            pdfFile.setUploadTime(LocalDateTime.now());
            pdfFile.setId(System.currentTimeMillis());

            pdfFileRepository.save(pdfFile);

            return new ResponseEntity<>("上传文件成功", HttpStatus.OK);
        } catch (IOException e) {
            e.printStackTrace();
            return new ResponseEntity<>("上传文件失败", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}
